//
//  LdAccountOrderViewController.m
//  爱鲜蜂订单界面
//
//  Created by 刘栋 on 16/9/24.
//  Copyright © 2016年 刘栋. All rights reserved.
//

#import "LdAccountOrderViewController.h"

#import <Masonry.h>
#import "AccountOrderDetailViewController.h"
#import "UIColor+Addition.h"
#import "NetworkingManager.h"

#define XScrennWidth [UIScreen mainScreen].bounds.size.width
#define XScrennHeight [UIScreen mainScreen].bounds.size.height
@interface LdAccountOrderViewController ()<UIScrollViewDelegate>

//底部的横线
@property (nonatomic,weak) UIImageView *lineView;

@property (nonatomic,weak) UIView *topView;

//滚动视图
@property (nonatomic,weak) UIScrollView *scrollView;

@end

@implementation LdAccountOrderViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    
    
    self.view.backgroundColor = [UIColor colorWithHex:0xefefef];
    
    //设置导航条
    [self setUpNavi];
    
    //设置界面
    [self setUpTopView];
    
    //下面是一个scrollView
    [self setUpScrollView];
    
    //下载网络数据
    [self loadData];
}

- (void)loadData
{
    NSString *urlString = @"http://iosapi.itcast.cn/loveBeen/MyOrders.json.php";
    
    NSMutableDictionary *params = [NSMutableDictionary dictionary];
    
    [params setObject:@"call" forKey:@"13"];
    
     [[NetworkingManager shareManager] POST:urlString parameters:params progress:nil success:^(NSURLSessionDataTask * _Nonnull task, id  _Nullable responseObject) {
         
         NSLog(@"%@",responseObject);
         
     } failure:^(NSURLSessionDataTask * _Nullable task, NSError * _Nonnull error) {
         
     }];
}

//设置滚动视图
- (void)setUpScrollView
{
     //创建滚动视图
    UIScrollView *scrollView = [[UIScrollView alloc] initWithFrame:CGRectZero];
    
    scrollView.pagingEnabled = YES;
    
    scrollView.scrollEnabled = NO;
    
    scrollView.delegate = self;
    scrollView.backgroundColor = [UIColor blueColor];
    [self.view addSubview:scrollView];
    
    //设置
    
    self.scrollView = scrollView;
    
    //设置约束
    [scrollView mas_makeConstraints:^(MASConstraintMaker *make) {
        
        make.top.equalTo(self.topView.mas_bottom).offset(10);
        make.left.right.bottom.equalTo(self.view);
        
    }];
    
    scrollView.contentSize = CGSizeMake(XScrennWidth * 4, XScrennHeight);
    
    
    //定义一个数组
    NSMutableArray *viewArray = [NSMutableArray array];
    
    for(int i=0;i<4;i++)
    {
        AccountOrderDetailViewController *acdv = [[AccountOrderDetailViewController alloc] init];
//        acdv.view.backgroundColor = [UIColor colorWithWhite:0.1*i alpha:1];
        acdv.number = i;
        acdv.view.backgroundColor = [UIColor randomColor];
        
        [scrollView addSubview:acdv.view];
        
        [viewArray addObject:acdv.view];
        
        //建立父子控制器关系
        [self addChildViewController:acdv];
    }
    
    //[scrollView layoutIfNeeded];
    
    //设置约束
    for(int i=0;i<viewArray.count-1;i++)
    {
        //设置约束
        UIView *currentView = viewArray[i];
        UIView *nextView = viewArray[i+1];
        
        if(i==0)
        {
             [currentView mas_makeConstraints:^(MASConstraintMaker *make) {
                
                 make.left.offset(0);
//                 make.width.offset(XScrennWidth);
//                 make.height.offset(XScrennHeight - 60);
//                 
             }];
//            [currentView mas_makeConstraints:^(MASConstraintMaker *make) {
//               
//                make.width
//                
//            }];
        }
        if(i==viewArray.count-2)
        {
            [nextView mas_makeConstraints:^(MASConstraintMaker *make) {
               
                make.right.offset(0);
                
            }];
        }
        [nextView mas_makeConstraints:^(MASConstraintMaker *make) {
           
            make.left.equalTo(currentView.mas_right);
//            make.width.equalTo(currentView.mas_width);
//            make.height.equalTo(currentView.mas_height);
        }];
    }
    
    //设置滚动上下边距
    [viewArray mas_makeConstraints:^(MASConstraintMaker *make) {
       
        make.bottom.top.offset(0);
        make.width.offset(XScrennWidth);
        make.height.offset(XScrennHeight - 124);
        
    }];
    
    //设置滚动范围
    //scrollView.contentSize = CGSizeMake(XScrennWidth * 4, XScrennHeight);
    
    
}

//设置顶部视图
- (void)setUpTopView
{
    //设置
    UIView *backView = [[UIView alloc] init];
    backView.backgroundColor = [UIColor whiteColor];
    [self.view addSubview:backView];
    
    self.topView = backView;
    
    //设置
    [backView mas_makeConstraints:^(MASConstraintMaker *make) {
       
        make.left.right.offset(0);
        make.top.offset(0);
        make.height.offset(50);
    }];
    
    //下面订单
    NSArray *titleArray = @[@"全部订单",@"待付款",@"待收货",@"待评价"];
    for(int i=0;i<4;i++)
    {
        UIButton *but = [UIButton buttonWithType:UIButtonTypeCustom];
        [but setTitle:titleArray[i] forState:UIControlStateNormal];
        
        [but setTitleColor:[UIColor blackColor] forState:UIControlStateNormal];
        [but setTitleColor:[UIColor redColor] forState:UIControlStateSelected];
        
        if(i==self.selectIndex)
        {
            but.selected = YES;
        }
        //设置字体大小
        but.titleLabel.font = [UIFont systemFontOfSize:14];
        
        [but addTarget:self action:@selector(butClick:) forControlEvents:UIControlEventTouchUpInside];
        but.titleLabel.textAlignment = NSTextAlignmentCenter;
        [backView addSubview:but];
        
        but.tag = 100+i;
    }
    
       //设置约束
    for(int i=0;i<backView.subviews.count-1;i++)
    {
        UIButton *currentBut  = backView.subviews[i];
        UIButton *nextBut = backView.subviews[i+1];
        
        if(i==0)
        {
          [currentBut mas_makeConstraints:^(MASConstraintMaker *make) {
             
              make.left.offset(0);
              
          }];
        }
        
        if(i==backView.subviews.count-2)
        {
            [nextBut mas_makeConstraints:^(MASConstraintMaker *make) {
               
                make.right.offset(0);
                
            }];
        }
        
        [nextBut mas_makeConstraints:^(MASConstraintMaker *make) {
           
            make.left.equalTo(currentBut.mas_right);
            make.width.equalTo(currentBut.mas_width);
        }];
    }
    
    [backView.subviews mas_makeConstraints:^(MASConstraintMaker *make) {
        
        make.top.offset(10);
        make.bottom.offset(-10);
        
    }];

    
    //创建底部红色view
    //获取到第一个按钮的 but
    UIButton *fisBut = backView.subviews[0];
    
    UIImageView *imageView = [[UIImageView alloc] init];
    imageView.backgroundColor = [UIColor redColor];
    [backView addSubview:imageView];
    self.lineView = imageView;
    //设置约束
    [imageView mas_makeConstraints:^(MASConstraintMaker *make) {
        
        make.height.offset(2);
        make.width.offset(55);
        make.centerX.equalTo(fisBut.mas_centerX);
        make.bottom.offset(0);
        
    }];
}

//按钮点击事件
- (void)butClick:(UIButton *)but
{
    for(int i=0;i<4;i++)
    {
        UIButton *but = [self.view viewWithTag:100+i];
        but.selected =NO;
    }
    but.selected = YES;
    
    //在这里必须重置约束
    [self.lineView mas_remakeConstraints:^(MASConstraintMaker *make) {
       
        make.height.offset(2);
        make.width.offset(55);
        make.centerX.equalTo(but.mas_centerX);
        make.bottom.offset(0);
    }];
    
//    self.scrollView.contentOffset = CGPointMake((but.tag-100)*XScrennWidth, 0);
    [self.scrollView setContentOffset:CGPointMake((but.tag-100)*XScrennWidth, 0) animated:YES];
    
}

- (void)viewWillAppear:(BOOL)animated
{
    [super viewWillAppear: animated];
    
    self.navigationController.navigationBar.hidden = NO;
    
    self.navigationController.navigationBar.translucent = NO;
}


- (void)setUpNavi
{
      self.title = @"我的订单";
    

}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
